def p(n):
    for i in range(2,n):
        if n%i==0:
            return False
    return True
x=int(input())
prost=[i for i in range(1,10000) if p(i)]

m=0

if 1000<=x<=9990:
        for i in prost:
            if str(i) in str(x):
                m=max(i,m)
print(m)
